class PIDController {
    constructor(Kp, Ki, Kd) {
        this.Kp = Kp; // Proportional gain
        this.Ki = Ki; // Integral gain
        this.Kd = Kd; // Derivative gain

        this.lastError = 0; // Store the previous error
        this.integral = 0; // Accumulated error
    }

    compute(controlVariable, setpoint) {
        const error = setpoint - controlVariable;

        // Proportional term
        const proportional = this.Kp * error;

        // Integral term
        this.integral += error;
        const integral = this.Ki * this.integral;

        // Derivative term
        const derivative = this.Kd * (error - this.lastError);
        this.lastError = error;

        // Compute the control output
        const output = proportional + integral + derivative;

        return output;
    }
}

export default PIDController;
